package ice.hufs.ac.kr.push;

import java.util.*;

import android.app.*;
import android.content.*;
import android.util.*;

import com.google.android.gcm.*;

public class GCMIntentService extends GCMBaseIntentService {
//	static String re_message;
	
	public static ArrayList<String> msgAL = new ArrayList<String>();
//	public static int count = 0;
//	static String[] valueCode;
//	static MainActivity ma;
	
	static String TAG = GCMIntentService.class.getName();
	/*
	 * 	for(int i = 0; i <=GCMIntentService.msgAL.size(); i++ ) {
			Log.e(TAG + mTag, "msgALSize = " + GCMIntentService.msgAL.size());
			Log.e(TAG + mTag, "msgALSize = " + GCMIntentService.msgAL.get(index));
	 */

	private static void generateNotification(Context context, String message) {
		String mTag = " generateNotification";
		int icon = R.drawable.ic_action_search;
		long when = System.currentTimeMillis();
//		valueCode = new String[100];
//		ma = new MainActivity();
		
		NotificationManager notificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		Notification notification = new Notification(icon, message, when);

		String title = context.getString(R.string.app_name);
		Intent notificationIntent = new Intent(context, MainActivity.class);
		msgAL.add(message);
		
		Log.e(TAG + mTag, "msgAL size == " + msgAL.size());
		
		for(int i = 0 ; i < msgAL.size(); i++) {
			Log.e(TAG + " generateNotification", msgAL.get(i));
			
		}
		
//		Log.e("count", "이전 count:" + count);
//		Log.e("valueCode", count + " : " + valueCode[count]);
//		valueCode[count] = re_message;
//		count++;
//		Log.e("count", "현재 count:" + count);
//		ma.valueTest = valueCode;
//		Log.e("re_message", "re_message:" + re_message);
//		Log.e("count", "현재 count:" + count);
//		Log.e("valueCode", count + " : " + valueCode[count]);
		
//		for(int i = 0 ; i < count ; i++)
//			Log.e("설마", valueCode[i]+ " ");
		//		ma.message.setText(re_message);
		//		notificationIntent.putExtra("tae", re_message);

		notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
		PendingIntent intent = PendingIntent.getActivity(context, 0,notificationIntent, 0);

		notification.setLatestEventInfo(context, title, message, intent);
		notification.flags |= Notification.FLAG_AUTO_CANCEL;
		notificationManager.notify(0, notification);
	}

	@Override
	protected void onError(Context arg0, String arg1) {

	}

	@Override
	protected void onMessage(Context context, Intent intent) {
		String msg = intent.getStringExtra("msg");
		Log.e("getmessage", "getmessage:" + msg);

		generateNotification(context, msg);
	}

	@Override
	protected void onRegistered(Context context, String reg_id) {
		Log.e("Ű�� ����մϴ�.(GCM INTENTSERVICE)", reg_id);
	}

	@Override
	protected void onUnregistered(Context arg0, String arg1) {
		Log.e("Ű�� �����մϴ�.(GCM INTENTSERVICE)", "���ŵǾ���ϴ�.");
	}
}